* File Information

  * Title: ModelingCSDHPreliminaryReplication.do
  * Date: July 2024
  * Stata Version: Stata 17
  * Purpose: Reproduce the results in Modeling CSDH for IPE Data (IJPE)
  
* Clear Stata
  clear
  
* Load Data

  * Working Directory
    "/Users/claywebb/Dropbox/farmseriescropsectional/Replication Materials/"
	
  * Load Data
    use "MilnerKubota_IO_replicationWithFX.dta"
	
* Table 1: TSCS Models of Statutory Tariff Rates  
  eststo clear
  
  eststo: reg   newtar l1polity  l1gdp_pc l1lnpop l1signed  l1office l1ecris2 l1bpc1 l1avnewtar date
  
  drop xb residuals 
  predict xb if e(sample)
  gen residuals = newtar-xb 
  xtserial residuals
  
  eststo: xtreg newtar l1polity  l1gdp_pc l1lnpop l1signed  l1office l1ecris2 l1bpc1 l1avnewtar date, fe
  
  drop xb residuals 
  predict xb if e(sample)
  gen residuals = newtar-xb 
  xtserial residuals

  eststo: xtreg newtar l1polity  l1gdp_pc l1lnpop l1signed  l1office l1ecris2 l1bpc1 l1avnewtar date, re
  
  drop xb residuals 
  predict xb if e(sample)
  gen residuals = newtar-xb 
  xtserial residuals
  
  eststo: xtpcse  newtar l1polity  l1gdp_pc l1lnpop l1signed  l1office l1ecris2 l1bpc1 l1avnewtar date, pairwise
  
  drop xb residuals 
  predict xb if e(sample)
  gen residuals = newtar-xb 
  xtserial residuals
  
  eststo: xi: xtpcse newtar l1polity  l1gdp_pc l1lnpop l1signed  l1office l1ecris2 l1bpc1 l1avnewtar date i.country, pairwise
  
  drop xb residuals 
  predict xb if e(sample)
  gen residuals = newtar-xb 
  xtserial residuals
  
  eststo: xi: xtpcse newtar l1polity  l1gdp_pc l1lnpop l1signed  l1office l1ecris2 l1bpc1 l1avnewtar date i.country, corr(ar1) pairwise

  drop xb residuals 
  predict xb if e(sample)
  gen residuals = newtar-xb 
  xtserial residuals
  
  esttab, nogaps nodep se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) mtitles(POLS FE RE PCSE PCSE-LSDV MK) coeflabels(l1polity "Polity" l1lnpop "LN Pop" l1gdp_pc "GDP PC" l1signed "IMF" l1office "Office" l1ecris2 "EC Crisis" l1bpc1 "BP Crisis" l1avnewtar "AV Tariff" date "Trend") nonumbers
 
  esttab using mktscs.tex, nogaps nodep se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) mtitles(POLS FE RE PCSE PCSE-LSDV MK) coeflabels(l1polity "Polity" l1lnpop "LN Pop" l1gdp_pc "GDP PC" l1signed "IMF" l1office "Office" l1ecris2 "EC Crisis" l1bpc1 "BP Crisis" l1avnewtar "AV Tariff" date "Trend") nonumbers
  
* Table 2: CSDH in TSCS Data

  * Load Data for Table 2
    use "TableTwoVariables.dta", replace
	
  * Set Directory for Plots
    cd "/Users/claywebb/Dropbox/farmseriescropsectional/Replication Plots/"
	
  * Inflation PACFs (Row One)
levelsof countrycode, local(names)

local infpacf

foreach s of local names { 
    // Check if there are observations for the current country
    count if countrycode == "`s'" & !missing(inflation)
    if r(N) > 0 {
        pac inflation if countrycode == "`s'", saving("infpacf`s'", replace) title("`s'") ytitle("Inflation")
        local infpacf `"`infpacf' infpacf`s'.gph"'   // Add .gph to each saved file
    }
}

graph combine `infpacf', saving("InflationPlot.gph", replace) title("Inflation by Country") 
graph export "CSDHInflation.png", replace

  * FDI Inflows PACFs (Row Two)
levelsof countrycode, local(names)

local fdiflowsinpacf

foreach s of local names { 
    // Check if there are enough observations for the current country
    count if countrycode == "`s'" & !missing(fdiinflows)
    if r(N) > 5 { // Only proceed if there are more than 5 observations
        pac fdiinflows if countrycode == "`s'", saving("fdiflowsinpacf`s'", replace) title("`s'") ytitle("FDI Inflows")
        local fdiflowsinpacf `"`fdiflowsinpacf' fdiflowsinpacf`s'.gph"'   // Add .gph to each saved file
    }
}

graph combine `fdiflowsinpacf', saving("FDIInflowsPlot.gph", replace) title("FDI Inflows by Country") 
graph export "CSDHFDIInflows.png", replace

  * FDI Outflows PACFs (Row Three)
levelsof countrycode, local(names)

local fdiflowsoutpacf

foreach s of local names { 
    // Check if there are enough observations for the current country
    count if countrycode == "`s'" & !missing(fdioutflows)
    if r(N) > 5 { // Only proceed if there are more than 5 observations
        pac fdioutflows if countrycode == "`s'", saving("fdiflowsoutpacf`s'", replace) title("`s'") ytitle("FDI Outflows")
        local fdiflowsoutpacf `"`fdiflowsoutpacf' fdiflowsoutpacf`s'.gph"'   // Add .gph to each saved file
    }
}

graph combine `fdiflowsoutpacf', saving("FDIOutflowsPlot.gph", replace) title("FDI Outflows by Country") 
graph export "CSDHFDIOutflows.png", replace

  * US Exchange Rate (Row Four)
levelsof countrycode, local(names)

local xratepacf

foreach s of local names { 
  // Check if there are enough observations for the current country
  count if countrycode == "`s'" & !missing(usdxrate)
  if r(N) > 5 { // Only proceed if there are more than 5 observations
    pac usdxrate if countrycode == "`s'", saving("xratepacf`s'", replace) title("`s'") ytitle("US Exchange Rate")
    local xratepacf `"`xratepacf' xratepacf`s'.gph"'   // Add .gph to each saved file
    }
}

graph combine `xratepacf', saving("USDXRatePlot.gph", replace) title("USD Exchange Rate by Country") 
graph export "CSDHUSDXRate.png", replace

  * Tariff Rate (Row Five)
levelsof countrycode, local(names)

local tariffspacf

foreach s of local names { 
  // Check if there are enough observations for the current country
  count if countrycode == "`s'" & !missing(tariffs)
  if r(N) > 5 { // Only proceed if there are more than 5 observations
    pac tariffs if countrycode == "`s'", saving("tariffspacf`s'", replace) title("`s'") ytitle("Tariff Rate")
    local tariffspacf `"`tariffspacf' tariffspacf`s'.gph"'   // Add .gph to each saved file
  }
}

graph combine `tariffspacf', saving("TariffPlot.gph", replace) title("Tariff Rate by Country") 
graph export "Tariff.png", replace

  * Expenditures (Row Six)
levelsof countrycode, local(names)

local spendpacf

foreach s of local names { 
  // Check if there are enough observations for the current country
  count if countrycode == "`s'" & !missing(gnexpenditures)
  if r(N) > 5 { // Only proceed if there are more than 5 observations
    pac gnexpenditures if countrycode == "`s'", saving("spendpacf`s'", replace) title("`s'") ytitle("Gross Expenditures")
    local spendpacf `"`spendpacf' spendpacf`s'.gph"'   // Add .gph to each saved file
}
}

graph combine `spendpacf', saving("TariffPlot.gph", replace) title("Gross Expenditures by Country") 
graph export "Expenditures.png", replace

  * Exports (Row Seven)
    levelsof countrycode, local(names)

local exportpacf

foreach s of local names { 
  // Check if there are enough observations for the current country
  count if countrycode == "`s'" & !missing(exports)
  if r(N) > 5 { // Only proceed if there are more than 5 observations
    pac exports if countrycode == "`s'", saving("exportpacf`s'", replace) title("`s'") ytitle("Gross Expenditures")
    local exportpacf `"`exportpacf' exportpacf`s'.gph"'   // Add .gph to each saved file
}
}

graph combine `exportpacf', saving("TariffPlot.gph", replace) title("Exports by Country") 
graph export "Exports.png", replace
  
* Table 3: Mixed Effects Models of Statutory Tariff Rates

  * Load Milner and Kubota
    cd "/Users/claywebb/Dropbox/farmseriescropsectional/Replication Materials/"
	
    use "MilnerKubota_IO_replicationWithFX.dta", replace

    eststo clear
  
  * MK 
	eststo: xtpcse newtar l1polity  l1gdp_pc l1lnpop l1signed  l1office l1ecris2 l1bpc1 l1avnewtar date i.country, corr(ar1) pairwise

    drop xb residuals 
    predict xb if e(sample)
    gen residuals = newtar-xb 
    xtserial residuals
  
  * Mixed Effects Model
	eststo: mixed newtar l.newtar l1polity  l1signed  l1office  l1gdp_pc l1lnpop   l1ecris2   l1bpc1  l1avnewtar date   || country: l.newtar

	estat ic

	drop e
	predict e, residuals
	xtserial e
  
  * Table
    esttab, nogaps nodep se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) coeflabels(L.newtar "Tariff Ratest-1"l1polity "Regime" 1aclpn "Dem" l1dictator1 "Dictator" l1sp2 "Single Party" l1milit2 "Military" l1lnpop "LN Pop" l1gdp_pc "GDP PC" l1signed "IMF" l1office "Office" l1ecris2 "EC Crisis" l1bpc1 "BP Crisis" l1avnewtar "AV Tariff" date "Trend" l1gatt_wto_new "GATT" l1fiveop "Five Open" l1usheg "US Heg" l1fdi "FDI" l _cons "Constant") drop(*.country) nomtitles scalars(r2)

	
